//
//  TTStatuesViewModel.swift
//  WeiBo
//
//  Created by 涂永江 on 2017/6/8.
//  Copyright © 2017年 涂永江. All rights reserved.
//

import UIKit

class TTStatuesViewModel: NSObject {

    var statues:TTStatusesModel?
    {
        didSet{
          
            guard let crruentModel = statues else {
                return
            }
            
            setMbrank(num: (crruentModel.user?.mbrank)!)
            setverifiedTypeImageView(num: (crruentModel.user?.verified_type)!)
            setcontenLabel(stustu: crruentModel)
            setReposts_count(num: crruentModel.reposts_count)
            setComments_count(num: crruentModel.comments_count)
            setAttitudes_count(num: crruentModel.attitudes_count)
            setScoure(str: crruentModel.source!)
            
        }
    
    }
    
    var mbrank:UIImage?
    var verifiedTypeImageView:UIImage?
    var contensText:String?
    var reposts_count:String?
    var comments_count:String?
    var attitudes_count:String?
    var scoure:String?
    var time:String?
    {
        get{
        
            guard let date1 = statues?.created_at else {
                return nil
            }
            
            let dft = DateFormatter()
            
            //如果是今年
            if isYear(year: date1)
            {
              //是不是今天
                  //创建日历对象
                let currcalner = Calendar.current
                if currcalner.isDateInToday(date1)
                {
                     //判断和当前时间相差多久
                      let result = abs(date1.timeIntervalSinceNow)
                       if result < 60
                       {
                         return "刚刚"
                        }else if result < 3600
                       {
                         let a = result/60
                        return "\(Int(a))分钟ago"
                    
                        }else
                       {
                        let b = result/3600
                        return "\(Int(b))小时ago"
                       }
                
                }else if currcalner.isDateInYesterday(date1)
                {
                //不是今天
                 dft.dateFormat = "昨天 HH:mm"
                }else
                {
                  dft.dateFormat = "MM-dd HH:mm"
                }
                
            }else
            {
            //如果不是今年
            dft.dateFormat = "yyyy-MM-dd HH:mm"
            }
            
            return dft.string(from: date1)
        }
    
    }
    
    
    //判断是不是今年
    func isYear(year:Date) ->Bool
    {
        let dft = DateFormatter()
        dft.dateFormat = "yyyy"
        let curretYear = dft.string(from: Date())
        let toWeetYear = dft.string(from: year)
        if curretYear == toWeetYear
        {
           return true
        }
        
       return false
    }
    
    func setScoure(str:String)
    {
    
        let arr:[String] = str.components(separatedBy:">")
        
        if arr.count>1{
        let str1 = arr[1]
           scoure = (str1 as NSString).substring(to: (str1 as NSString).length-3)
           scoure = "frome :\(scoure ?? "")"
        
        }else
        {
        scoure = ""
        }
        
    }

    func setAttitudes_count(num:Int){
        
      attitudes_count = common(str: "点赞", num: num)
        
    }
    
    func setComments_count(num:Int)
    {
   comments_count = common(str: "评论", num: num)
        
    }
    
    func common(str:String,num:Int) ->String
    {
    
        var name:String?
        
        if num == 0
        {
            name = str
            
        }
        if num > 0 && num < 10000
        {
            name = "\(num)"
        }
        if num == 10000
        {
            name = "\(num)w"
            
        }
        
        if num > 10000
        {
            let wan = num/10000
            var qian = num%10000
            if qian > 1000
            {
                qian = qian/1000
                name = "\(wan).\(qian)w"
            }else
            {
               name = "\(wan)w"
                
            }
            
            
        }
        
        if num>100000
        {
            
            let wan = num/10000
            name = "\(wan)w"
        }
        

        return name!
    }
    
    func setReposts_count(num: Int)
    {
    
    reposts_count = common(str: "转发", num: num)
        
    
    }
    
    func setMbrank(num:Int)
    {
    
        if num>=1 && num<=6{
        
            mbrank = UIImage(named:"common_icon_membership_level\(num)")
        
        }
    
    }
    
    func setverifiedTypeImageView(num:Int)
    {
    
        switch num {
        case 0:
            verifiedTypeImageView = UIImage(named: "avatar_vip")
        case 2 , 3, 5:
             verifiedTypeImageView = UIImage(named:"avatar_enterprise_vip")
        case 220:
            verifiedTypeImageView = UIImage(named:"avatar_grassroot")
        default:
            verifiedTypeImageView = nil
            
        }
    
    }
    
    func setcontenLabel(stustu:TTStatusesModel)
    {
      if stustu.retweeted_status != nil
      {
        if let name = statues?.retweeted_status?.user?.screen_name,let text = stustu.retweeted_status?.text{
            contensText = "@\(name):\(text)"
        }
      }
    
    }
    
}
